package com.demo.order_service.service.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.order_service.dao.OrderDao;
import com.demo.order_service.model.entity.Order;
import com.demo.order_service.service.ExcelService;
import com.demo.order_service.util.ExportExcelUtils;

/** * @author 付嘉星 
/** * ٩(*Ӧ)و写的全对
/** * 困到昏厥(✽ ﾟдﾟ ✽)  
/** * @date 创建时间：2018年4月20日 下午5:00:24 
/** * @version 1.0 */

@Service
public class ExcelServiceImpl  implements ExcelService  {

	private final OrderDao orderDao;
    
    /**
     * 构造函数
     *
     * @param orderDao
     */
	 public ExcelServiceImpl(@Autowired OrderDao orderDao) {
        this.orderDao = orderDao;
    }
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
			        List<Order> orderList=orderDao.selectAll();
			        request.getSession().setAttribute("userInfoList",orderList);
			     // 定义表的标题
			     			String title = "订单列表一览";

			     			// 定义表的列名
			     			String[] rowsName = new String[] {"序号", "订单ID号","订单时间", "商品ID", "商品数量", "商家名称", "商家地址", "订单金额", "付款方式", "订单状态", "下单人","收货地址","电话" };

			     			// 定义表的内容
			     			List<Object[]> dataList = new ArrayList<Object[]>();
			     			Object[] objs = null;
			     			for ( Order order: orderList) {
			     				System.out.println(order.getOid());
			     				objs = new Object[rowsName.length];
			     				objs[0] = "";
			     				objs[1] = order.getOid();
			     				objs[2] = order.getOtime();
			     				objs[3] = order.getCommodityid();
			     				objs[4] = order.getComamount();
			     				objs[5] = order.getMerchantname();
			     				objs[6] = order.getMerchantress();
			     				objs[7] = order.getOrdertotal();
			     				objs[8] = order.getPayment();
			     				objs[9] = order.getOstatus();
			     				objs[10] = order.getUname();
			     				objs[11] = order.getAddress();
			     				objs[12] = order.getPhone();
			     				dataList.add(objs);
			     				
			     			}
			     			System.out.println(dataList.toString());

			     			// 创建ExportExcelUtils公共类对象
			     			ExportExcelUtils ex = new ExportExcelUtils(title, rowsName, dataList);

			     			OutputStream outputStream = response.getOutputStream();
			     			response.setHeader("Content-disposition", "attachment; filename=OrderList.xls");
			     			response.setContentType("application/msexcel");

							try {
								ex.export(outputStream);
							} catch (Exception e) {
								// TODO 自动生成的 catch 块
								e.printStackTrace();
							}
			     			outputStream.close();		
	}
}
